/****************************************************************************
 * 2025.8 枯木逢春
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace Projectsurviva
{
    /// <summary>
    /// Controlller
    /// </summary>
    public partial class ExpupgradePanel : UIElement, IController
    {
        private void Awake()
        {
            var expupgradesystem = this.GetSystem<ExpSystem>();
            foreach (var item in expupgradesystem.Items)
            {
                BtnupgradePrefab.InstantiateWithParent(UpgradeRoot).Self(self =>
                {
                    var itemcache = item;
                  //  self.GetComponentInChildren<Text>().text = item.Description;
                    self.onClick.AddListener(() =>
                    {
                        Time.timeScale = 1;
                        itemcache.Upgrade();
                        this.Hide();
                        AudioKit.PlaySound("_ability_level_up");
                    });
                    var selfcache = self;
                  selfcache.Hide();
                    itemcache.Visible.RegisterWithInitValue((visible
                        ) =>
                    {
                        if(visible)
                        {
                            selfcache.Show();
                        }
                        else
                        {
                            selfcache.Hide();
                        }
                          
                    }).UnRegisterWhenGameObjectDestroyed(selfcache);
                    
                    itemcache.currentlevel.RegisterWithInitValue(lv =>
                    {
                        selfcache.GetComponentInChildren<Text>().text = itemcache.Description;
                    }).UnRegisterWhenGameObjectDestroyed(gameObject);
                });


            }
        }
               

        protected override void OnBeforeDestroy()
        {
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}